<template>
  <div>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="10">
        <el-col :span="4">
          <el-select
            v-model="queryInfo.department_id"
            clearable
            placeholder="请选择学院"
            @clear="getClassList()"
          >
            <el-option
              v-for="item in collegesOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getCollegeStageList()"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加阶段</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="collegeStage" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="阶段序号" prop="id"></el-table-column>
        <el-table-column label="阶段名称" prop="name"></el-table-column>
        <el-table-column label="学院" prop="college"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeRoleById(scope.row.id)"
            ></el-button>
            <el-tooltip
              effect="dark"
              content="排课"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="showCoursePlan(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改阶段的对话框 -->
    <el-dialog
      title="修改阶段信息"
      :visible.sync='editDialogVisible'
      width="50%"
      @close="editDialogVisible=false"
    >
     <el-form
        :model="editForm"
        :rules="FormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="阶段名称" prop="name" label-width="85px">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属学院" prop="college_id" label-width="85px">
          <el-select v-model="editForm.college_id" placeholder="请选择学院">
            <el-option
              v-for="item in collegesOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCollegeStage">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 添加阶段的对话框 -->
    <el-dialog
      title="添加阶段"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogVisible = false"
    >
      <el-form
        :model="addForm"
        :rules="FormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="阶段名称" prop="name" label-width="85px">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属学院" prop="college_id" label-width="85px">
          <el-select v-model="addForm.college_id" placeholder="请选择学院">
            <el-option
              v-for="item in collegesOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCollegeStage">确 定</el-button>
      </span>
    </el-dialog>
    
  </div>
</template>

<script>
import {getClass,createClass,createCoursePlan,getCoursePlan} from '../../api/class'
import {getColleges} from '../../api/department'

import {getCollegeStage,createCollegeStage,getCourseStageById,editCourseStageById} from '../../api/college_stage'
export default {
  data() {
    return {
      editDialogVisible:false,
      options: [],
      collegesOptions:[],
      queryInfo: {
        department_id: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5,
      },
      collegeStage: [],
      total: 0,

      // 添加表单数据
      addForm: {
        name: '',
        college_id: ''
      },
      editForm: {
        name: '',
        college_id: ''
      },
      // 添加、修改表单的验证规则对象
      FormRules: {
        name: [
          { required: true, message: '请输入阶段名称', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '阶段名称的长度在2~10个字符之间',
            trigger: 'blur',
          },
        ],
        school_id: [
          { required: true, message: '请选择学院', trigger: 'blur' },
        ],
      },
      
    }
  },
  created() {
    this.getCollegesOptions()
    this.getCollegeStageList()
  },
  methods: {
    //获取所有的学院信息
    async getCollegesOptions(){
      const result = await getColleges()
      this.collegesOptions=[]
      if(result){
        result.forEach(item => {
          this.collegesOptions.push({
            value:item.id,
            label:item.name
          })
        });
      }
    },    
    async getCollegeStageList() {
      const result = await getCollegeStage(this.queryInfo)
      this.collegeStage = result.data //列表中展示的数据内容
      this.total = result.count //数据总条数
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getClassList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getClassList()
    },
    //添加阶段
    addCollegeStage() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        // 可以发起添加用户的网络请求
        if (await createCollegeStage(this.addForm)) {
          this.addDialogClosed()
          this.getCollegeStageList()
          this.$message.success('添加阶段成功！')
          
        }
      })
    },

    // 展示编辑的对话框
    async showEditDialog(id) {
      const result = await getCourseStageById(id)
      if (result) {
        this.editForm = result
        this.editForm.level = Boolean(this.editForm.level)
        this.editDialogVisible = true
      }
    },
    // 修改课程阶段信息
    async editCollegeStage(){
      var res = await editCourseStageById(this.editForm.id,this.editForm)
      console.log(res)
      if (res){
        this.editDialogVisible = false
        this.$message.success('修改课程信息成功')
        this.getCollegeStageList()
      }

    },
    // 监听修改部门对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    // 修改部门信息并提交
    editDepartment() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        let id = this.editForm.id
        delete this.editForm.id
        this.editForm.level = Number(this.editForm.level)
        // 发起修改用户信息的数据请求
        const result = await editRoleItem(id, this.editForm)

        if (result) {
          // 关闭对话框
          this.editDialogVisible = false
          // 刷新数据列表
          this.getDeparList()
          // 提示修改成功
          this.$message.success('更新职位信息成功！')
        }
      })
    },
    // 根据Id删除对应的用户信息
    async removeRoleById(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该职位, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      removeRole(id).then((value) => {
        this.$message.success('删除职位成功！')
        this.getClassList()
      })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
